﻿package UIComponents// ActionScript file
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class BarMissions extends Sprite
	{
		private var _listMissions:Sprite;
		private var _nowPosition:int;
		private var _missionsAry:Array;
		
		private var db:Sprite;
		public function BarMissions():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._listMissions=new Sprite;
//			this.addChild(this.listMissions);
			this.addChildAt(this.listMissions,0);
			this.listMissions.y=5;
			this.addListener();
			this.changeButtonsVisibled(false);
			
			//向下按扭挡板。防冶按扭隐藏后点到任务上去
			db	=	new Sprite;
			db.graphics.beginFill(0x000000,0);
			db.graphics.drawRect(0,0,-(this.getChildByName("button_down") as MyButton).width,-(this.getChildByName("button_down") as MyButton).height);
			db.graphics.endFill();
			db.x	=	(this.getChildByName("button_down") as MyButton).x;
			db.y	=	(this.getChildByName("button_down") as MyButton).y;
		}
		
		private function addListener():void
		{
			(this.getChildByName("button_up") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,upHandler);
			(this.getChildByName("button_down") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,downHandler);
		}
		
		private function removeListener():void
		{
			(this.getChildByName("button_up") as MyButton).removeEventListener(ComponentEvent.BUTTON_DOWN,upHandler);
			(this.getChildByName("button_down") as MyButton).removeEventListener(ComponentEvent.BUTTON_DOWN,downHandler);
		}
		
		private function upHandler(e:ComponentEvent):void
		{
			if(this.nowPosition==0)return;
			this.nowPosition-=1;
			this.reshow();
		}
		
		private function downHandler(e:ComponentEvent):void
		{
			if(this.nowPosition+4>=this.missionsAry.length)return;
			this.nowPosition+=1;
			this.reshow();
		}
		
		private function changeButtonsVisibled(value:Boolean):void
		{
			this.getChildByName("button_up").visible=value;
			this.getChildByName("button_down").visible=value;
		}
		
		public function setMissionsAry(missionsAry:Array):void
		{
			this._missionsAry=missionsAry;
			this._nowPosition=0;
			if(this.missionsAry.length>4)
			{
				this.changeButtonsVisibled(true);
			}
			else
			{
				this.changeButtonsVisibled(false);
			}
			this.reshow();
		}
		
		private function reshow():void
		{
			this.reset();
			for(var i:int=0;i<4;i++)
			{
				var showMission:IMission=missionsAry[i+this.nowPosition];
				if(showMission)
				{
				    var missionIcon:DisplayObject=showMission.missionShowIcon;
				    this.listMissions.addChild(missionIcon);
					missionIcon.y=i*70;
				}
			}
			
			this.showupdownbtn();
		}
		
		private function reset():void
		{
			while(this.listMissions.numChildren)this.listMissions.removeChildAt(0);
		}
		
		public function release():void
		{
			
		}

		/*getter & setter*/
		public function get listMissions():Sprite
		{
			return _listMissions;
		}

		public function get missionsAry():Array
		{
			return _missionsAry;
		}

		public function get nowPosition():int
		{
			return _nowPosition;
		}

		public function set nowPosition(value:int):void
		{
			_nowPosition = value;
		}
		
		/**
		 *显示隐藏上下前头 
		 * 
		 */
		public function showupdownbtn():void
		{
			if(this.nowPosition+4>=this.missionsAry.length){
				(this.getChildByName("button_down") as MyButton).visible=false;
				this.addChild(db);
			}
			else{
				(this.getChildByName("button_down") as MyButton).visible=true;
				if( this.contains(db))this.removeChild(db);
			}
			
			
			if(this.nowPosition>0)(this.getChildByName("button_up") as MyButton).visible=true;
			else
				(this.getChildByName("button_up") as MyButton).visible=false;
		}


	}
}